package knowse.app.daos;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class TestStatusDetailDataObject implements Parcelable {
	
	private String testClass;
	private String method;
	private String message;
	private String type;
	private String location;
	private String backtrace;
	private Date timeStamp;
	
	
	//parsing JSON from JSONObject ..
	//Author: Xi
	
	public TestStatusDetailDataObject(JSONObject test) throws JSONException{
		//parsing....
		this.testClass=test.optString("class","null");
		this.method=test.optString("method","null");
		this.message=test.optString("message","null").replaceAll("\n", "");
		this.type=test.optString("type","null");
		this.location=test.optString("location","null");
		this.backtrace = test.optString("backtrace", "null");
		this.timeStamp = null;
	}
	
	/*
	 * 
	 */
	public TestStatusDetailDataObject(JSONObject test, String filename) throws JSONException, Exception{
		this.testClass=test.optString("class","null");
		this.method=test.optString("method","null");
		this.message=test.optString("message","null").replaceAll("\n", "");
		this.type=test.optString("type","null");
		this.location=test.optString("location","null");
		this.backtrace = test.optString("backtrace", "null");
		String timeStampString = extractTimeStamp(filename);
		this.timeStamp = timeStampStringToDate(timeStampString);
	}
	
	/*
	 * Convert a timestamp from String to Date.
	 * @param - Timestamp 
	 * @returns Timestamp in Date format
	 */
	private Date timeStampStringToDate(String timeStampString) throws ParseException {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
		return df.parse(timeStampString);
	}
	
	/*
	 * Extracts the timestamp from the given filename.
	 * @param filename - The filename of the test problem file.
	 * @returns The string representation of the timestamp in the given filename
	 */
	private String extractTimeStamp(String filename) throws Exception{
		String dateRegex = "[0-9]+_[0-9]{6}";
		Pattern p = Pattern.compile(dateRegex);
		Matcher m = p.matcher(filename);
		String timeStampString = null;
		if(m.find()){
			timeStampString = filename.substring(m.start(), m.end());
		}else{
			throw new Exception("TestStatusDetailDataObject: No timestamp in the filename");
		}
		return timeStampString;
	}
	
	//print TestDataObject
	//Author: Xi
	public void PrintOutTestStatusDetailDataObject(){
		System.out.println("testClass: " + testClass+ "\n"+
						  "method: " +method+"\n"+
						  "message: "+message+"\n"+
						  "type: "+type+"\n"+
						  "location: "+location+"\n");
	}
	
	
	public TestStatusDetailDataObject(String testClass, String method, String message, String type, String location)
	{
		this.testClass = testClass;
		this.method = method;
		this.message = message;
		this.type = type;
		this.location = location;
	}
	
	public TestStatusDetailDataObject() {
		// TODO Auto-generated constructor stub
	}

	public String getTimeStampString(){
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return df.format(this.timeStamp);
	}
	
	public Date getTimeStamp(){
		return this.timeStamp;
	}
	
	public void setTimeStamp(Date timeStamp){
		this.timeStamp = timeStamp;
	}
	
	
	public String getBacktrace(){
		return backtrace;
	}
	
	public void setBacktrace(String backtrace){
		this.backtrace = backtrace;
	}
	
	public String getTestClass() {
		return testClass;
	}



	public void setTestClass(String testClass) {
		this.testClass = testClass;
	}




	public String getMethod() {
		return method;
	}




	public void setMethod(String method) {
		this.method = method;
	}




	public String getMessage() {
		return message;
	}




	public void setMessage(String message) {
		this.message = message;
	}




	public String getType() {
		return type;
	}




	public void setType(String type) {
		this.type = type;
	}




	public String getLocation() {
		return location;
	}




	public void setLocation(String location) {
		this.location = location;
	}

	/*
	 * (non-Javadoc)
	 * @see android.os.Parcelable#describeContents()
	 */
	public int describeContents() {
		return 0;
	}

	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeString(testClass);
		parcel.writeString(method);
		parcel.writeString(message);
		parcel.writeString(type);
		parcel.writeString(location);
		parcel.writeString(backtrace);
		
	}
	
	public static final Parcelable.Creator<TestStatusDetailDataObject> CREATOR= new Creator<TestStatusDetailDataObject>(){

		public TestStatusDetailDataObject createFromParcel(Parcel source) {
			TestStatusDetailDataObject testStatusDetailDataObj= new TestStatusDetailDataObject();
			testStatusDetailDataObj.testClass=source.readString();
			testStatusDetailDataObj.method=source.readString();
			testStatusDetailDataObj.message=source.readString();
			testStatusDetailDataObj.type=source.readString();
			testStatusDetailDataObj.location=source.readString();
			testStatusDetailDataObj.backtrace=source.readString();
			return testStatusDetailDataObj;
		}

		public TestStatusDetailDataObject[] newArray(int size) {
			// TODO Auto-generated method stub
			return new TestStatusDetailDataObject[size];
		}};


	
	
	
}
